var server = require('../../utils/server');
var app = getApp();
Page({
	data: {
		filterId: 1,
		searchWords: '',
		placeholder: '烤鸭',
		shops: app.globalData.shops
	},
	onLoad: function () {
	},
	onShow: function () {
		//this.setData({
		//	showResult: false
		//});
	},
	inputSearch: function (e) {
		this.setData({
			searchWords: e.detail.value
		});
	},
	doSearch: function() {
    let self = this;
    server.postJSON('searchFood', {
      key: self.data.searchWords,
      pageIndex: 0 
    }, function (res) {
      if (res.data.status === 200 ) {
        self.setData({
          showResult: true,
          shops: res.data.data
        });
      }
    });
	},
	tapFilter: function (e) {
		switch (e.target.dataset.id) {
			case '1':
				this.data.shops.sort(function (a, b) {
					return a.id - b.id;
				});
				break;
			case '2':
				this.data.shops.sort(function (a, b) {
					return b.sales - a.sales;
				});
				break;
			case '3':
				this.data.shops.sort(function (a, b) {
					return a.distance - b.distance;
				});
				break;
		}
		this.setData({
			filterId: e.target.dataset.id,
			shops: this.data.shops
		});
	}
});

